//admin website
$(function(){
	admin_website.run();
});

var admin_website = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || "";
			this.sysId = $("#sysId").val();
			this.category = $("#category").val();
			this.countryCode = $("#countryCode").val();
		},
		
		_start_: function(){			
			this._list_websites();
		},
		
		_init_events: function(){
			//ready for dialog
			$("#new_website_div").admindialog({
				offsetLeft: -20,
				offsetTop: -50,
				gatherData: true,
				submit: function(data){
					var url = admin_website.contextPath + "/admin/homebanner/add";
					$.post(url, data, function(result){
						alert(result);
						location.reload(location.href);
					});
				}
			});
			
			//ready for dialog
			$("#update_website_div").admindialog({
				gatherData: true,
				submit: function(data){
					var url = admin_website.contextPath + "/admin/homebanner/update";
					$.post(url, data, function(result){
						alert(result);
						admin_website._list_websites();
					});					
				}
			});
			
			$("#add_website_btn").click(function(){				
				$("#new_website_div").admindialog('open');
			});
			
			$("#apply_website_btn").click(function(){
				var url = admin_website.contextPath + "/admin/homebanner/clean";
				$.get(url, function(result){					
					alert(result);					
				});
			});
			
			$("#upload_image_btn").click(function(){
				$("#upload_file_div").dialog({
					open: true,
					submit: function(data){
						
					}
				});
			});
					
			this._bind_category_events();
			this._bind_country_events();			
		},
		
		_bind_category_events: function(){
			$("#add_cat_btn").click(function(){
				var url = admin_website.contextPath + "/admin/website/cat/add";
				var data = $("#add_cat_form").serialize();
				$.post(url, data, function(result){
					alert(result);
					if(result.indexOf("Done") == 0){
						location.reload(true);
					}
				});
			});
			
			$("#category_table tr > td > a.delete").click(function(){
				var checked = window.confirm("Are you sure ?");
				if(checked){
					var id = $(this).attr("catId");
					var url = admin_website.contextPath + "/admin/website/cat/delete/" + id;
					$.get(url, function(result){
						alert(result);
						if(result.indexOf("Done") == 0){
							location.reload(true);
						}
					});
				}			
			});
		},
		
		_bind_country_events: function(){
			$("#add_country_btn").click(function(){
				var url = admin_website.contextPath + "/admin/website/country/add";
				var data = $("#add_country_form").serialize();
				$.post(url, data, function(result){
					alert(result);
					if(result.indexOf("Done") == 0){
						location.reload(true);
					}
				});
			});
			
			$("#country_table tr > td > a.delete").click(function(){
				var checked = window.confirm("Are you sure ?");
				if(checked){
					var id = $(this).attr("cid");
					var url = admin_website.contextPath + "/admin/website/country/delete/" + id;
					$.get(url, function(result){
						alert(result);
						if(result.indexOf("Done") == 0){
							location.reload(true);
						}
					});
				}			
			});
		},
		
		_list_websites: function(){
			var url = this.contextPath + "/admin/homebanner/list?countryCode=" + this.countryCode;
			if(this.sysId){
				url += "&sysId=" + this.sysId;
			}
			if(this.category){
				url += "&category=" + this.category;
			}
			$.get(url, function(result){
				$("#website_list_div").html(result);
				admin_website._bind_website_events();
			});
		},
		
		_bind_website_events: function(){
			$("#website_table  tr > td > a.update").click(function(){
				var id = $(this).attr("linkId");
				var url = admin_website.contextPath + "/admin/homebanner/get/" + id;
				$.get(url, function(result){
					$("#update_content_div").html(result);		
					$("#update_website_div").dialog('open');
				});
			});
			$("#website_table  tr > td > a.delete").click(function(){
				var checked = window.confirm("Are you sure ?");
				if(checked){
					var id = $(this).attr("linkId");
					var url = admin_website.contextPath + "/admin/homebanner/delete/" + id;
					$.get(url, function(result){
						admin_website._list_websites();
					});
				}				
			});
		}
};